// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CourseImportAction.java

package com.iphaeton.lms.action.course.scorm12;

import com.iphaeton.common.helper.IUploadHelper;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.course.scorm12.runtime.core.LMSManifestHandler;
import com.iphaeton.lms.course.scorm12.runtime.core.LMSPackageHandler;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import org.xml.sax.InputSource;

public class CourseImportAction extends Action {

	public CourseImportAction() {
		myManifestHandler = null;
		myPackageHandler = null;
		upload = null;
	}

	public IUploadHelper getUpload() {
		return upload;
	}

	public void setUpload(IUploadHelper upload) {
		this.upload = upload;
	}

	public LMSManifestHandler getMyManifestHandler() {
		return myManifestHandler;
	}

	public void setMyManifestHandler(LMSManifestHandler myManifestHandler) {
		this.myManifestHandler = myManifestHandler;
	}

	public LMSPackageHandler getMyPackageHandler() {
		return myPackageHandler;
	}

	public void setMyPackageHandler(LMSPackageHandler myPackageHandler) {
		this.myPackageHandler = myPackageHandler;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		daf = (DynaActionForm) form;
		String organize_id = "";
		String uploadDir = "";
		String theWebPath = request.getRealPath("/");
		String courseName = (String) daf.get("courseName");
		FormFile courseFile = (FormFile) daf.get("courseFile");
		String controlType = (String) daf.get("controlType");
		String sort = (String) daf.get("sort");
		String criterion = (String) daf.get("criterion");
		String comment = (String) daf.get("comment");
		Integer hour = (Integer) daf.get("period");
		String fileName = courseFile.getFileName();
		try {
			UserSession userSession = (UserSession) request.getSession()
					.getAttribute("userSession");
			String createrId = userSession.getUserId();
			organize_id = userSession.getOrgId();
			String sessionID = request.getSession().getId();
			uploadDir = File.separatorChar + "tempUploadDir"
					+ File.separatorChar + sessionID;
			logger.info("\u8BFE\u4EF6\u89E3\u538B\u7F29\u8DEF\u5F84:"
					+ uploadDir);
			upload.upload(courseFile, uploadDir + File.separatorChar);
			fileName = uploadDir + File.separatorChar + fileName;
			logger.info("\u8BFE\u4EF6\u6587\u4EF6\u8DEF\u5F84:" + fileName);
			LMSPackageHandler.extract(fileName, "imsmanifest.xml", uploadDir);
			String manifestFile = uploadDir + File.separatorChar
					+ "imsmanifest.xml";
			InputSource fileToParse = setUpInputSource(manifestFile);
			myManifestHandler.setCourseName(courseName);
			myManifestHandler.setFileToParse(fileToParse);
			myManifestHandler.setControl(controlType);
			myManifestHandler.setHour(hour);
			myManifestHandler.setOrganizeId(organize_id);
			myManifestHandler.setCreater(createrId);
			myManifestHandler.setSort(sort);
			myManifestHandler.setCriterion(criterion);
			myManifestHandler.setComment(comment);
		} catch (BusinessException e1) {
			e1.printStackTrace();
			request.setAttribute("exception", e1);
			return mapping.findForward("failure");
		} catch (NullPointerException ne) {
			return mapping.findForward("session_dead");
		}
		boolean result = myManifestHandler.processManifest();
		String courseID = myManifestHandler.getCourseID();
		logger.info("\u8BFE\u7A0BID:" + courseID);
		ZipFile archive = new ZipFile(fileName);
		byte buffer[] = new byte[16384];
		for (Enumeration e = archive.entries(); e.hasMoreElements();) {
			ZipEntry entry = (ZipEntry) e.nextElement();
			if (!entry.isDirectory()) {
				String filename = entry.getName();
				filename = filename.replace('/', File.separatorChar);
				filename = theWebPath + "course" + File.separator + courseID
						+ File.separator + filename;
				File destFile = new File(filename);
				String parent = destFile.getParent();
				if (parent != null) {
					File parentFile = new File(parent);
					if (!parentFile.exists())
						parentFile.mkdirs();
				}
				InputStream in = archive.getInputStream(entry);
				OutputStream outStream = new FileOutputStream(filename);
				int count;
				while ((count = in.read(buffer)) != -1)
					outStream.write(buffer, 0, count);
				in.close();
				outStream.close();
			}
		}

		String sequencingFileName = theWebPath + "\\course\\sequence.obj";
		File sequencingFile = new File(sequencingFileName);
		FileOutputStream ostream = new FileOutputStream(sequencingFile);
		ObjectOutputStream oos = new ObjectOutputStream(ostream);
		oos.writeObject(myManifestHandler.getOrgsCopy());
		oos.flush();
		oos.close();
		File tempFile = new File(uploadDir);
		File uploadFiles[] = tempFile.listFiles();
		logger.info("\u4E34\u65F6\u6587\u4EF6\u8DEF\u5F84:" + uploadDir);
		logger.info("\u4E34\u65F6\u6587\u4EF6\u6570\u91CF:"
				+ uploadFiles.length);
		for (int i = 0; i < uploadFiles.length; i++)
			uploadFiles[i].deleteOnExit();

		tempFile.deleteOnExit();
		request.setAttribute("cid", courseID);
		request.setAttribute("oid", organize_id);
		return mapping.findForward("privateCourse");
	}

	private InputSource setUpInputSource(String fileName) {
		InputSource is = new InputSource();
		is = setupFileSource(fileName);
		return is;
	}

	private InputSource setupFileSource(String filename)
    {
        InputSource is;
        try {
        	 File xmlFile = new File(filename);
             if(xmlFile.isFile()){
             	  FileReader fr = new FileReader(xmlFile);
                   is = new InputSource(fr);
                   return is;
             }
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("Exception" + e);
		}
       
          
      
      
      
     
      
    
        return new InputSource();
    }

	protected LMSManifestHandler myManifestHandler;
	protected LMSPackageHandler myPackageHandler;
	protected IUploadHelper upload;
	private static Logger logger;
	private DynaActionForm daf;

	static {
		logger = Logger
				.getLogger(com.iphaeton.lms.action.course.scorm12.CourseImportAction.class
						.getName());
	}
}
